//
//  XQTabBarItem.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/18.
//

#import "XQTabBarItem.h"
#import <Masonry/View+MASAdditions.h>

@interface XQTabBarItem()
@property (nonatomic, strong) UIButton *btn;
@end

@implementation XQTabBarItem


- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image selectedImage:(nullable UIImage *)selectedImage {
    self = [super init];
    if (self) {
        self.normalTextColor = [UIColor lightGrayColor];
        self.selectedTextColor = [UIColor redColor];
        
        [self addSubview:self.btn];
        [self addSubview:self.titleLabel];
        [self addSubview:self.imageView];
        
        [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
        }];
        
        if (![title isEqualToString:@""]) {
            [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                (void)make.centerX;
                make.bottom.offset(-3);
            }];
        }
        
       
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerX;
            make.top.offset(11);
            make.width.and.height.equalTo(@30);
        }];
        self.titleLabel.text = title;
        self.imageView.image = image;
        self.normalImage = image;
        self.selectedImage = selectedImage;
    }
    return self;
}

- (void)setSelected:(BOOL)selected {
    if (_selected && selected) {
        return;
    }
    _selected = selected;
    self.titleLabel.textColor = selected ? self.selectedTextColor : self.normalTextColor;
    
    if (self.type == XQTabBarItemTypeNormal) {
        self.imageView.image = selected ? self.selectedImage : self.normalImage;
    } 
}

- (void)setNormalTextColor:(UIColor *)normalTextColor {
    _normalTextColor = normalTextColor;
    self.titleLabel.textColor = self.selected ? self.selectedTextColor : normalTextColor;
}

- (void)setSelectedTextColor:(UIColor *)selectedTextColor {
    _selectedTextColor = selectedTextColor;
    self.titleLabel.textColor = self.selected ? selectedTextColor : self.normalTextColor;
}

- (void)setNormalImage:(UIImage *)normalImage {
    _normalImage = normalImage;
    self.imageView.image = self.selected ? self.selectedImage : normalImage;
}

- (void)setSelectedImage:(UIImage *)selectedImage {
    _selectedImage = selectedImage;
    self.imageView.image = self.selected ? selectedImage : self.normalImage;
}

- (void)setType:(XQTabBarItemType)type {
    _type = type;
    if (type == XQTabBarItemTypeNormal) {
        [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerX;
            make.top.offset(8);
            make.width.and.height.equalTo(@30);
        }];
        [self.titleLabel setHidden:NO];
    }else if (type == XQTabBarItemTypeImage) {
        [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerX;
            make.top.offset(8);
            make.width.and.height.equalTo(@22);
        }];
        [self.titleLabel setHidden:NO];
    } else {
        [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.offset(0);
            make.top.offset(-5);
            make.bottom.offset(-3);
        }];
        [self.titleLabel setHidden:YES];
    }
}

- (void)buttonClick{
    if (self.singleTapHandle) {
        self.singleTapHandle(self);
    }
}

#pragma mark - Getters

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = self.normalTextColor;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [UIImageView new];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imageView;
}

- (UIButton *)btn {
    if (!_btn) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.backgroundColor = [UIColor clearColor];
        @weakify(self);
        [_btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}


@end
